<template>
    <div class="ai-container">
      <div class="input-group">
        <input
          v-model="inputPrompt"
          @keyup.enter="sendRequest"
          placeholder="输入你的问题"
          class="input-field"
          :disabled="loading"
        >
        <button 
          @click="sendRequest"
          class="send-button"
          :disabled="!inputPrompt.trim() || loading"
        >
          {{ loading ? '发送中...' : '发送' }}
        </button>
      </div>
  
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
        <span class="loading-text">思考中...</span>
      </div>
  
      <div v-if="errorMessage" class="error-message">
        <span class="error-icon">⚠️</span>
        {{ errorMessage }}
      </div>
  
      <div v-if="responseData" class="response-container">
        <div class="response-header">AI回复：</div>
        <pre class="response-content">{{ responseData }}</pre>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue';
  import axios from 'axios';
  
  const inputPrompt = ref('');
  const responseData = ref(null);
  const errorMessage = ref(null);
  const loading = ref(false);
  
  const appId = '929f981cd2d147458760b6f61cfc8e54';
  const apiKey = 'sk-326b05c04a66472c9502dacec202cde9';
  
  const sendRequest = async () => {
    const input = prompt('请输入密码');
    if (input !== '123456789') {
      alert('密码错误');
      return;
    }
    if (!inputPrompt.value.trim()) return;
  
    loading.value = true;
    errorMessage.value = null;
    responseData.value = null;
  
    const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
    const headers = {
      'Authorization': `Bearer ${apiKey}`,
      'Content-Type': 'application/json'
    };
    const data = {
      "input": {
        "prompt": inputPrompt.value
      },
      "parameters": {},
      "debug": {}
    };
  
    try {
      const response = await axios.post(url, data, { headers });
      responseData.value = response.data;
    } catch (error) {
      errorMessage.value = `请求出错: ${error.response?.data?.message || error.message}`;
    } finally {
      loading.value = false;
    }
  };
  </script>
  
  <style scoped>
  .ai-container {
    max-width: 800px;
    margin: 2rem auto;
    padding: 2rem;
    background: #f8f9fa;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  }
  
  .input-group {
    display: flex;
    gap: 12px;
    margin-bottom: 1.5rem;
  }
  
  .input-field {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    font-size: 16px;
    transition: border-color 0.3s ease;
  }
  
  .input-field:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
  }
  
  .send-button {
    padding: 12px 24px;
    background: #007bff;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 500;
    transition: background 0.3s ease;
  }
  
  .send-button:disabled {
    background: #a0a0a0;
    cursor: not-allowed;
  }
  
  .send-button:not(:disabled):hover {
    background: #0056b3;
  }
  
  .loading-container {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 1rem;
    margin: 1rem 0;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 8px;
  }
  
  .loading-spinner {
    width: 24px;
    height: 24px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid #007bff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }
  
  @keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
  }
  
  .loading-text {
    color: #666;
    font-size: 0.9em;
  }
  
  .error-message {
    padding: 1rem;
    margin: 1rem 0;
    background: #ffe3e3;
    color: #dc3545;
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
  }
  
  .error-icon {
    font-size: 1.2em;
  }
  
  .response-container {
    margin-top: 1.5rem;
    padding: 1.5rem;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  }
  
  .response-header {
    color: #007bff;
    font-weight: 600;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid #f0f0f0;
  }
  
  .response-content {
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.6;
    margin: 0;
    color: #333;
  }
  </style>